#include "dbtime.h"
#include "exceptions.h"

DBTime::DBTime()
{
	Fill ( 1, 1, 2000, 0, 0, 0, 0 );
}

DBTime::~DBTime()
{
	//dtor
}



void DBTime::Fill ( u8 d, u8 m, u16 y, u8 h, u8 mi, u8 s, u16 ms, i32 dst )
{
	while ( ms >= 1000 )
	{
		s ++;
		ms -= 1000;
	}

	tm t;

	t.tm_mday = d;
	t.tm_mon = m - 1;
	t.tm_year = y - 1900;
	t.tm_hour = h;
	t.tm_min = mi;
	t.tm_sec = s;
	t.tm_isdst = dst;
	dt.milisecond = ms;
    dt.time = mktime ( &t );

}


void DBTime::FillTime ( u8 h, u8 mi, u8 s, u16 ms, i32 dst )
{
	while ( ms >= 1000 )
	{
		s ++;
		ms -= 1000;
	}

	tm t;
	t = *localtime(&dt.time);

	t.tm_hour = h;
	t.tm_min = mi;
	t.tm_sec = s;
	t.tm_isdst = dst;
	dt.time = mktime ( &t );
	dt.milisecond = ms;
}


db_time DBTime::Serialize()
{
	return dt;
}

i32 DBTime::Deserialize ( db_time time )
{
	dt = time;
	return 1;
}

i32 DBTime::FromSystemTime ( time_t tt )
{
	dt.milisecond = 0;
	dt.time = tt;
	return 1;
}

time_t DBTime::ToSystemTime ()
{
	return dt.time;
}

db_specTime DBTime::translate()
{

    tm *info = localtime(&dt.time);
    if (!info)
    {
        EXCEX(0xAB01,"translate error");
    }

    db_specTime st;
    st.day = info->tm_mday;
    st.month = info->tm_mon + 1;
    st.year = info->tm_year + 1900;
    st.hour = info->tm_hour;
    st.minute = info->tm_min;
    st.second = info->tm_sec;
    st.milisecond = dt.milisecond;

    return st;
}

void DBTime::Actual()
{
	dt.milisecond = 0;
	dt.time = time(0);
}

string DBTime::TimeDateToString ( time_t &t)
{
	DBTime ttime;
	ttime.FromSystemTime ( t );

	return ttime.TimeDateToString();
}

string DBTime::TimeToString ( time_t &t  )
{
	DBTime ttime;
	ttime.FromSystemTime ( t );

	return ttime.TimeToString();
}

string DBTime::DateToString ( time_t &t  )
{
	DBTime ttime;
	ttime.FromSystemTime ( t );

	return ttime.DateToString();
}

string DBTime::TimeDateToString()
{
	string ret;
	tm *tinfo;
	tinfo = localtime ( &dt.time );
	static char buff[256];
	strftime ( buff, 256, "%H:%M:%S %d-%m-%y", tinfo );
	ret = buff;
	return ret;
}

string DBTime::DateToString()
{
	string ret;
	tm *tinfo;
	tinfo = localtime ( &dt.time);
	static char buff[256];
	strftime ( buff, 256, "%d-%m-%y", tinfo );
	ret = buff;
	return ret;
}


string DBTime::TimeToString()
{
	string ret;
	tm *tinfo;
	tinfo = localtime ( &dt.time );
	static char buff[256];
	strftime ( buff, 256, "%H:%M:%S", tinfo );
	ret = buff;
	return ret;
}

//time_t addTime ( time_t base, time_t value )
//{
//	tm *info;
//	info = localtime ( &base );
//
//	if ( !info )
//	{
//		LOGP3 ( "###addTime - error base (%d:%d)", base, value );
//		return 0;
//	}
//
//	i32 flag = info->tm_isdst;
//	base += value;
//	info = localtime ( &base );
//
//	if ( !info )
//	{
//		LOGP3 ( "###addTime - error value (%d:%d)", base, value );
//		return 0;
//	}
//
//	i32 flag2 = info->tm_isdst;
//
//	if ( ( flag == 1 ) && ( flag2 == 0 ) ) {
//		base += HOUR;
//	}
//
//	return base;
//}
/*
time_t  DBTime::calculateTimeChangeDateSummer ( u32 year )
{
	time_t x;// = time ( 0 );
	tm info;// = *localtime ( &x );
	info.tm_year = year - 1900;
	info.tm_mon = 3 - 1;
	info.tm_mday = 31;
	info.tm_hour = 16;
	info.tm_isdst = 0;
	info.tm_hour = 1;
	info.tm_min = 59;
	info.tm_sec = 59;
	x = mktime ( &info );

	while ( info.tm_wday )
	{
		info.tm_mday --;
		x = mktime ( & info );
	}

	x = mktime ( & info );
//	LOGP3 ( "DEBUG: Switch date in %d year to summer time is %s", year, DS_TD ( x ) );
	return x;
}

time_t  DBTime::calculateTimeChangeDateWinter ( u32 year )
{
	time_t x;// = time ( 0 );
	tm info;// = *localtime ( &x );
	info.tm_year = year - 1900;
	info.tm_mon = 10 - 1;
	info.tm_mday = 31;
	info.tm_isdst = 1;
	info.tm_hour = 1;
	info.tm_min = 59;
	info.tm_sec = 59;
	x = mktime ( &info );

	while ( info.tm_wday )
	{
		info.tm_mday --;
		x = mktime ( & info );
	}

	x = mktime ( & info );
//	x += HOUR;
//	LOGP5 ( "DEBUG: Switch date in %d year to winter time is %s\n %s %d", year, DS_TD ( x ) , ctime (&x),x );
	return x;
}

time_t gtime()
{
    time_t tnow = time(0);
    tm *t = gmtime( & tnow );
    tnow = mktime( t );
    return tnow;
}

*/










